package com.exp.quick_exec;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.AbstractFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.util.Assert;

import com.exp.base.util.PathUtils;
import com.exp.base.util.StringUtils;

public class QuickBuilder {
	String sourceModule; //下滑线
	String targetModule; //下滑线
	String sourceEntity; //大驼峰
	String targetEntity; //大驼峰
	

	
	public String getSourceModule() {
		return sourceModule;
	}

	public QuickBuilder setSourceModule(String sourceModule) {
		this.sourceModule = sourceModule;
		return this;
	}

	public String getTargetModule() {
		return targetModule;
	}

	public QuickBuilder setTargetModule(String targetModule) {
		this.targetModule = targetModule;
		return this;
	}

	public String getSourceEntity() {
		return sourceEntity;
	}

	public QuickBuilder setSourceEntity(String sourceEntity) {
		this.sourceEntity = sourceEntity;
		return this;
	}

	public String getTargetEntity() {
		return targetEntity;
	}

	public QuickBuilder setTargetEntity(String targetEntity) {
		this.targetEntity = targetEntity;
		return this;
	}

	public void exec() throws IOException{
		Assert.notNull(sourceModule,"sourceModule不能为空");
		Assert.notNull(targetModule,"targetModule不能为空");
		Assert.notNull(sourceEntity,"sourceEntity不能为空");
		Assert.notNull(targetEntity,"targetEntity不能为空");
		
		sourceModule = StringUtils.toUnderScoreCase(StringUtils.strip(sourceModule, "/").replace(".", "/"));
		targetModule = StringUtils.toUnderScoreCase(StringUtils.strip(targetModule, "/").replace(".", "/"));
		sourceEntity = StringUtils.toCapitalizeCamelCase(StringUtils.strip(sourceEntity, "/"));
		targetEntity = StringUtils.toCapitalizeCamelCase(StringUtils.strip(targetEntity, "/"));
		
		copyJavaFiles("src/main/java/com/exp");
		copyOtherFiles("src/main/webapp/WEB-INF/template");
		copyOtherFiles("src/main/webapp/resources/js");
		copyOtherFiles("src/main/webapp/resources/css");
	}

	private void copyJavaFiles(String FromPath) throws IOException {
		String currDir = PathUtils.getCwdDirPath();
		String sourceJavaDir = FilenameUtils.separatorsToUnix(currDir+"/"+FromPath+"/"+sourceModule);
		String targetJavaDir = FilenameUtils.separatorsToUnix(currDir+"/"+FromPath+"/"+targetModule);
		new File(targetJavaDir).mkdirs();
		Iterator<File> ite = FileUtils.iterateFilesAndDirs(new File(sourceJavaDir), new AbstractFileFilter() {
			@Override
			public boolean accept(File file) {
				if(file.isDirectory()){
					return true;
				}else if(file.isFile()){
					if(StringUtils.startsWithIgnoreCase(file.getName(), sourceEntity)){
						return true;
					}else{
						return false;
					}
				}
				return false;
			}
		},TrueFileFilter.TRUE );
		while(ite.hasNext()){
			File file = ite.next();
			String filePath = FilenameUtils.separatorsToUnix(file.getAbsolutePath());
			String fileSubPath = filePath.replace(sourceJavaDir, "");
			String targetPath = targetJavaDir + fileSubPath ;
			targetPath = replaceForTargetName(targetPath);
			System.out.println("-------------");
			System.out.println(file);
			System.out.println(targetPath);
			if(file.isDirectory()){
				new File(targetPath).mkdirs();
			}else if(file.isFile()){
				String content = FileUtils.readFileToString(file, "utf-8");
				content = replaceForTargetContent(content);
				FileUtils.writeStringToFile(new File(targetPath), content, "utf-8");
			}
		}
	}
	
	private void copyOtherFiles(String FromPath) throws IOException {
		String currDir = PathUtils.getCwdDirPath();
		String sourceJavaDir = FilenameUtils.separatorsToUnix(currDir+"/"+FromPath+"/"+sourceModule);
		String targetJavaDir = FilenameUtils.separatorsToUnix(currDir+"/"+FromPath+"/"+targetModule);
		new File(targetJavaDir).mkdirs();
		Iterator<File> ite = FileUtils.iterateFilesAndDirs(new File(sourceJavaDir), new AbstractFileFilter() {
			@Override
			public boolean accept(File file) {
				if(file.isDirectory()){
					return true;
				}else if(file.isFile()){
					String _file = StringUtils.toUnderScoreCase(file.getName());
					String _targetModule = StringUtils.toUnderScoreCase(sourceEntity);
					if(StringUtils.startsWithIgnoreCase(_file, _targetModule )){
						return true;
					}else{
						return false;
					}
				}
				return false;
			}
		},TrueFileFilter.TRUE);
		while(ite.hasNext()){
			File file = ite.next();
			String filePath = FilenameUtils.separatorsToUnix(file.getAbsolutePath());
			String fileSubPath = filePath.replace(sourceJavaDir, "");
			String targetPath = targetJavaDir + fileSubPath ;
			targetPath = replaceForTargetName(targetPath);
			System.out.println("-------------");
			System.out.println(file);
			System.out.println(targetPath);
			if(file.isDirectory()){
				new File(targetPath).mkdirs();
			}else if(file.isFile()){
				String content = FileUtils.readFileToString(file, "utf-8");
				content = replaceForTargetContent(content);
				FileUtils.writeStringToFile(new File(targetPath), content, "utf-8");
			}
		}
	}
	
	private String replaceForTargetName(String targetPath){
		String _targetPath = targetPath
				.replace(sourceModule.replace("/", "."), targetModule.replace("/", "."))
				.replace(StringUtils.capitalize(sourceModule), StringUtils.capitalize(targetModule))
				.replace(StringUtils.uncapitalize(sourceModule), StringUtils.uncapitalize(targetModule))
				.replace(StringUtils.toUnderScoreCase(sourceModule), StringUtils.toUnderScoreCase(targetModule))
				.replace(StringUtils.capitalize(sourceEntity), StringUtils.capitalize(targetEntity))
				.replace(StringUtils.uncapitalize(sourceEntity), StringUtils.uncapitalize(targetEntity))
				.replace(StringUtils.toUnderScoreCase(sourceEntity), StringUtils.toUnderScoreCase(targetEntity))
				;
		return _targetPath;
	}
	
	private String replaceForTargetContent(String targetPath){
		String _targetPath = targetPath
				.replace(sourceModule.replace("/", "."), targetModule.replace("/", "."))
				.replace(StringUtils.toCapitalizeCamelCase(sourceModule), StringUtils.toCapitalizeCamelCase(targetModule))
				.replace(StringUtils.toCamelCase(sourceModule), StringUtils.toCamelCase(targetModule))
				.replace(StringUtils.toUnderScoreCase(sourceModule), StringUtils.toUnderScoreCase(targetModule))
				.replace(StringUtils.toCapitalizeCamelCase(sourceEntity), StringUtils.toCapitalizeCamelCase(targetEntity))
				.replace(StringUtils.toCamelCase(sourceEntity), StringUtils.toCamelCase(targetEntity))
				.replace(StringUtils.toUnderScoreCase(sourceEntity), StringUtils.toUnderScoreCase(targetEntity))
				.replace(sourceModule.split("/")[0]+"_"+StringUtils.toUnderScoreCase(targetEntity),StringUtils.toUnderScoreCase(targetModule)+"_"+StringUtils.toUnderScoreCase(targetEntity));
		return _targetPath;
	}
	
/*	
	public static void main(String[] args) throws IOException {
		new QuickBuilder().setSourceModule("backend").setTargetModule("back")
		.setSourceEntity("entityExample").setTargetEntity("nima")
		.exec();
	}
*/	
}
